package com.rxt.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.rxt.common.result.Result;
import com.rxt.model.domain.FFinance;
import com.rxt.model.vo.FinanceVo;
import com.rxt.service.IFFinanceService;
import com.rxt.util.BaseContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周梓文
 * @since 2023-08-24
 */
@Api(tags = "融资模块")
@RestController
@RequestMapping("/fFinance")
public class FFinanceController {

    @Autowired
    private IFFinanceService financeService;

    @ApiOperation("新增融资信息")
    @PostMapping("/add")
    public Result add(@RequestBody FinanceVo vo) {
        System.out.println(vo);
        FFinance one = financeService.getOne(
                new LambdaQueryWrapper<FFinance>().eq(FFinance::getUserId, BaseContext.get().getId()));
        if (one != null) {
            return Result.fail("已参与融资");
        }
        FFinance fFinance = new FFinance();
        fFinance.setBankId(vo.getBankId());
        fFinance.setUserId(BaseContext.get().getId());
        fFinance.setRealName(vo.getName());
        fFinance.setPhone(vo.getPhone());
        fFinance.setStatus(0);
        fFinance.setMoney(vo.getAmount());
        fFinance.setRate(vo.getRate());
        fFinance.setCombinationIdnum1(vo.getIdentityNum());
        fFinance.setRepayment(vo.getRepayment());
        StringBuilder fileInfo = new StringBuilder();
        vo.getImgs().forEach(item -> fileInfo.append(item).append(","));
        fFinance.setFileInfo(fileInfo.toString());
        financeService.save(fFinance);
        return Result.ok();
    }

    @ApiOperation("获取用户个人的融资信息")
    @GetMapping("/own")
    public Result getUserFinance() {
        FFinance one = financeService.getOne(
                new LambdaQueryWrapper<FFinance>().eq(FFinance::getUserId, BaseContext.get().getId()));
        return Result.ok(one);
    }

    @ApiOperation("查询银行的融资申请信息")
    @GetMapping("/list")
    public Result list(Long id) {
        List<FFinance> finances = financeService.list(new LambdaQueryWrapper<FFinance>().eq(FFinance::getBankId, id));
        return Result.ok(finances);
    }

    @ApiOperation("修改融资申请状态")
    @GetMapping("/status")
    public Result modStatus(Long id, Integer status) {
        financeService.update(new LambdaUpdateWrapper<FFinance>()
                        .set(FFinance::getStatus, status)
                        .eq(FFinance::getId, id));
        return Result.ok();
    }
}

